import { Controller, Get, Post, Body, Patch, Param, Delete } from '@nestjs/common';
import { DepartmentService } from './department.service';
import { CreateDepartmentDto } from './dto/create-department.dto';
import { UpdateDepartmentDto } from './dto/update-department.dto';
import { buildTree } from 'src/utils/fun';

@Controller('department')
export class DepartmentController {
  constructor(private readonly departmentService: DepartmentService) {}

  @Post('save')
  async create(@Body() createDepartmentDto: CreateDepartmentDto) {
    const result = await this.departmentService.create(createDepartmentDto);
    return { 
      message: result ? '操作成功': '失败'
    }
  }

  @Get('load')
  async findAll() {
    const result = await this.departmentService.findAll();
    if(result){
      return {
        data: buildTree(result)
      }
    }
  }

  @Get('delete/:id')
  async delete(@Param('id') id: string) {
    const result = await this.departmentService.delete(id);
    return { 
      message: result ? '删除成功': '失败'
    }
  }
}
